//author Dreena Biswas

package controller;
import view.*;
import model.*;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.io.File;

//The Controller coordinates interactions between the View and Model

public class Controller {
	private View objectV;
	private Model objectM;
	private String name;
	
		public Controller(final View objectV, Model objectM) {
		this.objectV = objectV;
		this.objectM = objectM;
		
		//REFRESH LIST
		View.refresh.addActionListener(new ActionListener() {
			  
			   @Override
	           public void actionPerformed(ActionEvent arg0) {
				
					Model obj = new Model();
					System.out.println("Hello");
					try {
						View.list.setListData(Model.displayAllContact().toArray());
						for(int i=0; i<500000; i++)
						{
							System.out.println("refreshing contact count: "+i);
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					View.list.updateUI();
					System.out.println("Hello");
					
	     }
	     });
		
		
		//ADD CONTACT
		View.addButton.addActionListener(new ActionListener() {
			  
				@Override
				public void actionPerformed(ActionEvent arg0) {
			    NewFrame.createFrame1();	
			    
			    NewFrame.submitButton4.addActionListener(new ActionListener() {
					  
					@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Model obj = new Model();
				Contact cont = new Contact();
				
				cont.set_fName(NewFrame.fNameEntry.getText());
				cont.set_lName(NewFrame.lNameEntry.getText());
				cont.set_group(NewFrame.groupEntry.getText());
				cont.set_homeAdd(NewFrame.homeAddEntry.getText());
				cont.set_compAdd(NewFrame.compAddEntry.getText());
				cont.set_personalMobile(NewFrame.personalMobileEntry.getText());
				cont.set_personalLand(NewFrame.personalLandEntry.getText());
				cont.set_homePage(NewFrame.homePageEntry.getText());
				cont.set_emailId(NewFrame.emailIdEntry.getText());
				cont.set_photo(NewFrame.photoEntry.getText());
				cont.set_date(NewFrame.dateEntry.getText());
				
				obj.addContact(cont);		
												
				JOptionPane.showMessageDialog(null, "Successfully Added!");
				NewFrame.getAddframe().setVisible(false);
			
					}
					
		    });
		}
		});
		
		 
				
		//DELETE CONTACT
		View.deleteButton.addActionListener(new ActionListener() {
			  
			   @Override
	           public void actionPerformed(ActionEvent arg0) {
				
				NewFrame.createFrame2();
				
				NewFrame.submitButton3.addActionListener(new ActionListener() {
					  
					@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Model obj = new Model();
				Contact cont = new Contact();
				
				cont.set_fName(NewFrame.fNameEntry.getText());
				cont.set_lName(NewFrame.lNameEntry.getText());
				try {
					obj.deleteContact(cont);
					
					JOptionPane.showMessageDialog(null, "Successfully Deleted!");
					NewFrame.getDelframe().setVisible(false);
									
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}				
		    });	
			
	     }
	     });
		
			
		//EDIT CONTACT
		View.editButton.addActionListener(new ActionListener() {
			  
				@Override
		        public void actionPerformed(ActionEvent arg0) {
			   final JFrame frame = NewFrame.createFrame3();
			   
		        
			    NewFrame.submitButton.addActionListener(new ActionListener() {
					  
					@Override
				public void actionPerformed(ActionEvent arg0) {
					frame.dispose();
					Model obj = new Model();
					Contact cont = new Contact();
					cont.set_fName(NewFrame.fNameEntry.getText());
					cont.set_lName(NewFrame.lNameEntry.getText());
					try {
						Contact cont2 = obj.editContact1(cont);	
						System.out.println(cont2.get_fName());
						
						final JFrame frame2 = NewFrame.createFrame_3(cont2);
						

						} 
					catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();}										
			     }
			    });
		}

		});  
		
		
		//Sort by name
		NewFrame.submitButton2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<String> contact = new ArrayList<String>();
				ArrayList<String> sortlist = new ArrayList<String>();
				try {
					contact =  Model.sortbyName();
					Collections.sort(contact, String.CASE_INSENSITIVE_ORDER);
					for (String s : contact){
						if (s.charAt(0) == NewFrame.letterEntry.getText().toString().charAt(0)){
						 System.out.println(s);
						 sortlist.add(s);
						 
						}
							
					}
					NewFrame.createFrame_sortbyName(sortlist);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		});
		
		NewFrame.submitButton1.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
			    System.out.println(NewFrame.fNameEntry.getText());
			    System.out.println(NewFrame.lNameEntry.getText());
			    try {
			    	Contact cont = new Contact();
			    	cont.set_fName(NewFrame.fNameEntry.getText());
			    	cont.set_lName(NewFrame.lNameEntry.getText());
			    	cont.set_group(NewFrame.groupEntry.getText());
			    	cont.set_homeAdd(NewFrame.homeAddEntry.getText());
			    	cont.set_compAdd(NewFrame.compAddEntry.getText());
			    	cont.set_personalMobile(NewFrame.personalMobileEntry.getText());
			    	cont.set_personalLand(NewFrame.personalLandEntry.getText());
			    	cont.set_profMobile(NewFrame.profMobileEntry.getText());
			    	cont.set_profLand(NewFrame.profLandEntry.getText());
			    	cont.set_homePage(NewFrame.homePageEntry.getText());
			    	cont.set_emailId(NewFrame.emailIdEntry.getText());
			    	cont.set_photo(NewFrame.photoEntry.getText());
			    	cont.set_date(NewFrame.dateEntry.getText());
			    	
			    	Model.editContact2(cont);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			    
				
		}});
		  
		//SORT NAMEWISE
		View.sortName.addActionListener(new ActionListener() {
			  
			    @Override
	            public void actionPerformed(ActionEvent arg0) {
		        NewFrame.createFrame4();
	     }

	     }); 
		
		//Sort by name
				NewFrame.submitButton2.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent arg0) {
						ArrayList<String> contact = new ArrayList<String>();
						ArrayList<String> sortlist = new ArrayList<String>();
						try {
							contact =  Model.sortbyName();
							Collections.sort(contact, String.CASE_INSENSITIVE_ORDER);
							for (String s : contact){
								if (s.charAt(0) == NewFrame.letterEntry.getText().toString().charAt(0)){
								 System.out.println(s);
								 sortlist.add(s);
								 
								}
									
							}
							NewFrame.createFrame_sortbyName(sortlist);
							
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
				});
		
		
		//SORT GROUPWISE
		View.sortGroup.addActionListener(new ActionListener() {
			  
			@Override
			public void actionPerformed(ActionEvent arg0) {
			NewFrame.createFrame5();
			NewFrame.submitButton.addActionListener(new ActionListener() {
				  
				@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Model obj = new Model();
				try {
					ArrayList<String> grp = new ArrayList<String>(); 
					grp = obj.sortGroup(NewFrame.groupEntry.getText());
					System.out.println(grp);
					NewFrame.createFrame_5(grp);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				}
			});	
			}
		});
		
	

		
		//SORT DATEWISE
		 View.sortDate.addActionListener(new ActionListener() {
			  			 	
				@Override
				public void actionPerformed(ActionEvent arg0) {
			    ArrayList<Contact> cont = new ArrayList<Contact>();
			    Model obj = new Model();
			    try {
					cont = obj.getAllContactObjects();
					
					NewFrame.createFrame_date(cont);
					
					for(int i=0; i<cont.size(); i++)
					{
						System.out.println(cont.get(i).get_fName()+" "+cont.get(i).get_date());
					}
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					//NewFrame.createFrame6();
		}

		});
		 
		 
		 //DISPLAY CONTACT DETAILS
		 View.displayButton.addActionListener(new ActionListener() {
			  
				@Override
				public void actionPerformed(ActionEvent arg0) {
			    NewFrame.createFrame7();
			    NewFrame.submitButton.addActionListener(new ActionListener() {
					  
					@Override
				public void actionPerformed(ActionEvent arg0) {
					
					Model obj = new Model();
					try {
						ArrayList<String> cont = new ArrayList<String>(); 
						cont = obj.displayContact(NewFrame.fNameEntry.getText(), NewFrame.lNameEntry.getText());
						System.out.println(cont);
						NewFrame.createFrame_7(cont);
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			     }
			    });
				}
		 });

		 
		 //DISPLAY ALL
		 View.list.addListSelectionListener(
				 new ListSelectionListener(){
					 public void valueChanged(ListSelectionEvent event)
					 {
						
						 Model obj = new Model();
						ArrayList<String> cont = new ArrayList<String>();
						Object name = View.list.getSelectedValue();
						String[] nm = name.toString().split(" ");
						try {
							cont = obj.displayContact(nm[0], nm[1]);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						//System.out.println(cont);
						if (!event.getValueIsAdjusting()){NewFrame.createFrame_7(cont);}
					 }
			 
		 }); 
		 
		 //DISPLAY and FIRE
		/* NewFrame.list.addListSelectionListener(
				 new ListSelectionListener(){
					 public void valueChanged(ListSelectionEvent event)
					 {
						 StringBuilder sb = new StringBuilder();
						 StringBuilder sb2 = new StringBuilder();
						 
						 if (NewFrame.list.getSelectedIndex() == 10)
						 {
							 try {
									Desktop.getDesktop().mail(new URI( sb.append("mailto:").append(NewFrame.list.getSelectedValue().toString()).toString().replaceAll("Email id :     ", "")));
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (URISyntaxException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} 
						 }
							
						
						if (NewFrame.list.getSelectedIndex() == 9)
						{ 
							try {
								Desktop.getDesktop().browse(new URI( sb2.append(NewFrame.list.getSelectedValue().toString()).toString().replaceAll("Home Page Link :     ", "")));
						} catch (IOException | URISyntaxException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					 }
						
					}
						
		 }); */
		 
		}// end of constructor
		
} //end of class